//
//  NewsViewController.m
//  Menu
//
//  Created by Lynn-Shin on 15/7/3.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "NewsViewController.h"

@interface NewsViewController ()

@end

@implementation NewsViewController

-(void)dealloc{
    
    [_newsTableV release];
    [_allNewsArray release];
    [_HUD release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.newsTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 375*WIDTH, 667*HEIGHT-64-49) style:UITableViewStylePlain];
    [self.view addSubview:self.newsTableV];
    [self.newsTableV release];
    
    self.newsTableV.delegate = self;
    self.newsTableV.dataSource = self;
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"loading";
    [self.HUD show:YES];
    
    self.allNewsArray = [NSMutableArray array];
    
    [[DataHandle shareData] openDataBase];
    [[DataHandle shareData] createNewsCacheTable];
    self.allNewsArray = [[DataHandle shareData] selectNewsCacheTable];
    
    self.page = 1;
    self.isUp = YES;
    [self downLoadData];
    
    
    [self.newsTableV addLegendHeaderWithRefreshingBlock:^{
        self.isUp = YES;
        self.page = 1;
//        NSLog(@"*****%ld",self.page);
        [self downLoadData];
        [self.newsTableV.footer resetNoMoreData];
        [self.newsTableV.header endRefreshing];
    }];
    
    [self.newsTableV addLegendFooterWithRefreshingBlock:^{
        self.isUp = NO;
        self.page++;
//        NSLog(@"%ld",self.page);
        
        if (self.page == self.maxRefresh) {
            
            [self.newsTableV.footer noticeNoMoreData];
            
        } else {
            
            [self downLoadData];
            [self.newsTableV.footer endRefreshing];
        }
    
    }];
    
    
    
    // Do any additional setup after loading the view.
}

-(void)downLoadData{
    
    NSString *get = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/found/tblFresh!getTblFreshList.do?is_traditional=0&page=%ld&pageRecord=10&phonetype=1",self.page];
    
    [AFNTool get:get Body:nil HttpResult:JSON success:^(id result) {
        
        if (self.page == 1) {
            
            [[DataHandle shareData] dropNewsCacheTable];
            [[DataHandle shareData] createNewsCacheTable];
        }
        
        if (self.isUp) {
//            [[DataHandle shareData] dropNewsCacheTable];
            [self.allNewsArray removeAllObjects];
        }
        
        self.maxRefresh = [[[result objectForKey:@"bean"] objectForKey:@"maxPageNumber"] integerValue] + 1;
        
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            
            NewsListModel *news = [[NewsListModel alloc] initWithDic:dic];
            [self.allNewsArray addObject:news];
            [[DataHandle shareData] insertNewsCacheTable:news];
            [news release];
        }
        self.netOK = YES;
        [self.newsTableV reloadData];
        [self.HUD hide:YES];
    } failure:^(NSError *error) {
        
        self.HUD.labelText = @"加载失败";
        [self.HUD hide:YES afterDelay:1];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.allNewsArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"myCell";
    
    NewsTableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIndentifier];
    
    if (cell == nil) {
        
        cell = [[[NewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier] autorelease];
    }
    cell.newsList = [self.allNewsArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 110*HEIGHT;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (!self.netOK) {
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"请求失败";
        [self.HUD show:YES];
        [self.HUD hide:YES afterDelay:0.5];
        return;
    }
    
    NewsDetailViewController *newsDetailVC = [[NewsDetailViewController alloc] init];
    
    NewsListModel *newsListModel = [self.allNewsArray objectAtIndex:indexPath.row];
    newsDetailVC.navigationItem.title = newsListModel.name;
    newsDetailVC.freshId = newsListModel.freshId;
    
    [self.navigationController pushViewController:newsDetailVC animated:YES];
    [newsDetailVC release];
    
    [self.newsTableV deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
